﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVCUMPlatform.HelperLibrary
{
    /// <summary>
    /// 日期工具类
    /// </summary>
    public static class DateTimeHelper
    {
        /// <summary>
        /// yyyy-MM-dd
        /// </summary>
        /// <returns></returns>
        public static string ToYMD(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// yyyy-MM-dd HH:mm:ss
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ToYMDHMS(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// yyyy-MM-dd HH:mm:ss
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ToNum(this DateTime dateTime)
        {
            return dateTime.ToString("yyyyMMddHHmmss");
        }

        /// <summary>
        /// yyyy-MM-dd HH:mm:ss fff
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ToOrderNum(this DateTime dateTime)
        {
            return dateTime.ToString("yyyyMMddHHmmssfff");
        }
        /// <summary>
        /// yyyy-MM-dd HH:mm:ss
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ToYMDHM(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd HH:mm");
        }

        /// <summary>
        /// HH:mm
        /// </summary>
        /// <returns></returns>
        public static string ToHHmm(this DateTime dateTime)
        {
            return dateTime.ToString("HH:mm");
        }

        /// <summary>
        /// 时间转换为时间戳
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static long ToUnixTime(this DateTime date)
        {
            if (date == DateTime.MinValue)
            {
                return 0;
            }
            DateTime startTime = new DateTime(1970, 1, 1).ToLocalTime();
            long timeStamp = (long)(date - startTime).TotalSeconds; // 相差秒数
            return timeStamp;
        }

        /// <summary>
        /// 求两个日期间隔时间
        /// </summary>
        /// <param name="dateTime1"></param>
        /// <param name="dateTime2"></param>
        /// <returns></returns>
        public static double DateDiff(DateTime dateTime1, DateTime dateTime2, TimeType timeType = TimeType.Minute)
        {
            TimeSpan ts1 = new TimeSpan(dateTime1.Ticks);
            TimeSpan ts2 = new TimeSpan(dateTime2.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();

            double result = 0;
            switch (timeType)
            {
                case TimeType.Day:
                    result = ts.TotalDays;
                    break;
                case TimeType.Hour:
                    result = ts.TotalHours;
                    break;
                case TimeType.Minute:
                    result = ts.TotalMinutes;
                    break;
                case TimeType.Second:
                    result = ts.TotalSeconds;
                    break;
                default:
                    result = ts.TotalMinutes;
                    break;
            }

            return result;
        }

        /// <summary>
        /// 分钟转换为小时分钟字符串
        /// </summary>
        /// <param name="minutes"></param>
        /// <returns></returns>
        public static string MinutesToHourStr(int minutes)
        {
            return $"{minutes / 60}小时{minutes % 60}分钟";
        }

        /// <summary>
        /// 获取中文星期
        /// </summary>
        /// <param name="ENDayOfWeek"></param>
        /// <returns></returns>
        public static string GetCNDayOfWeek(string ENDayOfWeek)
        {
            switch (ENDayOfWeek)
            {
                case "Monday":
                    return "星期一";
                case "Tuesday":
                    return "星期二";
                case "Wednesday":
                    return "星期三";
                case "Thursday":
                    return "星期四";
                case "Friday":
                    return "星期五";
                case "Saturday":
                    return "星期六";
                case "Sunday":
                    return "星期日";
                default:
                    throw new Exception("Invalid ENDayOfWeek");
            }
        }

        /// <summary>
        /// 默认星期数组
        /// </summary>
        /// <returns></returns>
        public static List<int> DefaultWeek()
        {
            return new List<int> { 1, 2, 3, 4, 5, 6, 7 };
        }
        /// <summary>
        /// 获取中文星期
        /// </summary>
        /// <param name="ENDayOfWeek"></param>
        /// <returns></returns>
        public static string GetNumOfWeek(int ENDayOfWeek)
        {
            switch (ENDayOfWeek)
            {
                case 1:
                    return "星期一";
                case 2:
                    return "星期二";
                case 3:
                    return "星期三";
                case 4:
                    return "星期四";
                case 5:
                    return "星期五";
                case 6:
                    return "星期六";
                case 7:
                    return "星期日";
                default:
                    throw new Exception("Invalid ENDayOfWeek");
            }
        }

        /// <summary>
        /// 获取数字星期
        /// </summary>
        /// <param name="ENDayOfWeek"></param>
        /// <returns></returns>
        public static int GetNumOfWeek(string ENDayOfWeek)
        {
            switch (ENDayOfWeek)
            {
                case "Monday":
                    return 1;
                case "Tuesday":
                    return 2;
                case "Wednesday":
                    return 3;
                case "Thursday":
                    return 4;
                case "Friday":
                    return 5;
                case "Saturday":
                    return 6;
                case "Sunday":
                    return 7;
                default:
                    throw new Exception("Invalid ENDayOfWeek");
            }
        }

        /// <summary>
        /// 获取日期所在周的某一天的日期
        /// </summary>
        /// <param name="date"></param>
        /// <param name="weekDay"></param>
        /// <returns></returns>
        public static DateTime GetWeekDay(DateTime date, int weekDay)
        {
            if (date.DayOfWeek == 0)
            {
                date = date.AddDays(-1);
            }
            return date.AddDays((-(int)date.DayOfWeek) + weekDay);
        }

        public static bool IsDate(string strDate)
        {
            try
            {
                DateTime.Parse(strDate);  //不是字符串时会出现异常
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static double CalcTotalMilliseconds(string dateTime) 
        {
            if (string.IsNullOrWhiteSpace(dateTime) || !IsDate(dateTime) || DateTime.Compare(Convert.ToDateTime(dateTime), DateTime.Now)<0)
            {
                return 0;
            }
            var totalMilliseconds = Convert.ToDateTime(dateTime).Subtract(DateTime.Now).TotalMilliseconds;
            return totalMilliseconds;
        }
    }

    /// <summary>
    /// 时间类型枚举
    /// </summary>
    public enum TimeType
    {
        /// <summary>
        /// 天
        /// </summary>
        Day = 3,
        /// <summary>
        /// 小时
        /// </summary>
        Hour = 4,
        /// <summary>
        /// 分钟
        /// </summary>
        Minute = 5,
        /// <summary>
        /// 秒
        /// </summary>
        Second = 6
    }

    /// <summary>
    /// 日期类型
    /// </summary>
    public enum DateTimeType
    {
        /// <summary>
        /// 今天
        /// </summary>
        Yesterday = 1,
        /// <summary>
        /// 最近7天
        /// </summary>
        Latest7Days = 2,
        /// <summary>
        /// 最近1个月
        /// </summary>
        Latest1Month = 3,
        /// <summary>
        /// 一学期
        /// </summary>
        LarestTerm = 4,
        /// <summary>
        /// 自定义
        /// </summary>
        CustomTime = 5
    }
}
